<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="机器人名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入机器人名称" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="编号" prop="id" align="center"/>
      <el-table-column label="机器人名称" prop="name" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="机器人状态" prop="status" align="center" :formatter="stateFormat"/>
      <el-table-column label="交易对" prop="symbol" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="累计收益(U)" prop="profit" align="center"/>
      <el-table-column label="累计收益率(%)" prop="profitRatio" align="center"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="getTrades(scope.row)" v-hasPermi="['trade:profit:trades']">交易记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="1000px">
      <el-table v-loading="loading" :data="tradeList">
        <el-table-column label="编号" prop="id" align="center"/>
        <el-table-column label="订单号" prop="orderId" align="center"/>
        <el-table-column label="交易对" prop="symbol" align="center"/>
        <el-table-column label="交易类型" prop="tradeType" align="center"/>
        <el-table-column label="价格" prop="price" align="center"/>
        <el-table-column label="数量" prop="quantity" align="center"/>
        <el-table-column label="成交金额" prop="amount" align="center"/>
        <el-table-column label="错误信息" prop="error" align="center"/>
        <el-table-column label="创建时间" prop="createdTime" align="center">
            <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
            </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {searchProfit, getTrades} from "@/api/trade/profit";

export default {
  name: "Profit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收益表格数据
      dataList: [],
      // 交易记录数据
      tradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    stateFormat(row, column) {
      if (row.status === '0') {
        return '正常'
      } 
      if (row.status === '1') {
        return '暂停'
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      searchProfit(this.queryParams).then(
        response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 获取交易记录 */
    getTrades(row) {
      const id = row.id || this.ids;
      getTrades(id).then(response => {
        this.tradeList = response.rows;
        this.open = true;
        this.title = "交易记录";
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>